package cn.hn.tx.rebot.blog.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hn.comm.Tools;
import cn.hn.comm.ibatis.BaseDAO;
import cn.hn.tx.rebot.blog.model.Blog;
import cn.hn.tx.rebot.blog.model.Comment;
import cn.hn.tx.rebot.blog.service.IRebotBlogService;

public class RebotBlogServiceImpl implements IRebotBlogService {
	final String NAMESPACE = "cn.hn.tx.rebot.blog.dao";

	// DAO基类
	private BaseDAO baseDAO;


	/**
	 * 根据状态&评论得到日志
	 */
	public List<Blog> queryRebotWriteBlogByStatus_Comment(Integer status)
	{
		return baseDAO.queryList(NAMESPACE, "queryRebotWriteBlogByStatus_Comment", status);
	}
	
	/**
	 * 插入日志并处理超链接
	 */
	public void insertRebotWriteBlog_HandleHref(String keyword, String href,
			String title, String content, String url, int taskType,
			Date insertDate, Date updateDate, int status) {
		insertRebotWriteBlog(title, Tools.addHrefToKeyword(Tools
				.replaceAllHref(title, href), keyword, href), Tools
				.addHrefToKeyword(Tools.replaceAllHref(content, href), keyword,
						href), url, taskType, insertDate, updateDate, status);
	}

	public void insertRebotWriteBlog(String orgTitle, String title,
			String content, String url, int taskType, Date insertDate,
			Date updateDate, int status) {
		Blog blog = new Blog();

//		Map params = new HashMap<String, Object>();
//		params.put("ORGTITLE", orgTitle);
		blog.setOrgTitle(orgTitle);
//		params.put("TITLE", title);
		blog.setTitle(title);
//		params.put("CONTENT", content);
		blog.setContent(content);
//		params.put("URL", url);
		blog.setUrl(url);
//		params.put("TASK_TYPE", taskType);
		blog.setTask_type(taskType);
//		params.put("INSERT_TIME", insertDate);
		blog.setInsert_time(insertDate);
		if (updateDate != null) {
//			params.put("UPDATE_TIME", updateDate);
			blog.setUpdate_time(updateDate);
		}
//		params.put("STATUS", status);
		blog.setStatus(status);
		System.out.println("insert url=" + url);
		baseDAO.insert(NAMESPACE, "insertRebotWriteBlog", blog);

	}

	/**
	 * 标题一样的日志
	 */
	public List<Map<String, Object>> queryListRebotWriteBlog(String orgTitle) {
		return baseDAO
				.queryList(NAMESPACE, "queryListRebotWriteBlog", orgTitle);
	}

	public Map<String, Object> queryRebotWriteBlog(int id) {
		return (Map<String, Object>) baseDAO.query(NAMESPACE,
				"queryRebotWriteBlog", id);
	}
	
	public List<Blog> queryAllRebotWriteBlog()
	{
		return baseDAO.queryList(NAMESPACE, "queryAllRebotWriteBlog", null);
	}

	public void updateRebotWriteBlog(Blog blog) {
		baseDAO.update(NAMESPACE, "updateRebotWriteBlog", blog);
	}

	public void updateUserBlogHot() {
		baseDAO.update(NAMESPACE, "updateUserBlogHot", null);
	}

	public List<Blog> queryListRebotWriteBlogByStatus(int status, int limit) {
		Map params = new HashMap<String, Object>();
		params.put("STATUS", status);
		params.put("LIMIT", limit);
		return (List<Blog>) baseDAO.queryList(NAMESPACE,
				"queryListRebotWriteBlogByStatus", params);
	}

	public void insertRebotComment(Comment comment) {
		baseDAO.insert(NAMESPACE, "insertRebotComment", comment);

	}
	public void updateRebotComment(Comment comment)
	{
		baseDAO.update(NAMESPACE, "updateRebotComment", comment);
	}

	public List<Comment> queryListRebotComment(int bid, int status) {
		Map params = new HashMap<String, Object>();
		params.put("bid", bid);
		params.put("status", status);		
		return baseDAO.queryList(NAMESPACE, "queryListRebotComment", params);
	}

	public List<Comment> queryListRebotCommentByBid(Integer bid)
	{
		return baseDAO.queryList(NAMESPACE, "queryListRebotCommentByBid", bid);
	}
	
	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

}
